﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Diagnostics;
using System.Linq;
using EnvDTE;
using TskComment.Core;
using TskComment.Infra;

namespace TskComment.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        #region Properties and Fields

        [ImportMany(typeof(BaseComment))]
        public IEnumerable<BaseComment> Comments;

        private CompositionContainer _container;

        private ObservableCollection<BaseComment> _cmtCollection = new ObservableCollection<BaseComment>();
        public ObservableCollection<BaseComment> CMTCollection
        {
            get { return _cmtCollection; }
            set { _cmtCollection = value; RaisePropertyChanged("CMTCollection"); }
        }

        private BaseComment selectedItem = null;
        public BaseComment SelectedItem
        {
            get { return selectedItem; }
            set { selectedItem = value; RaisePropertyChanged("SelectedItem"); }
        }

        public DelegateCommand MoveToTopOfBlockCmd { get; set; }
        public DelegateCommand MoveToBottomOfBlockCmd { get; set; }
        public DelegateCommand ExecuteCmd { get; set; }

        public DTE DTE { get; set; }

        public TextSelection Selection
        {
            get
            {
                return DTE.ActiveDocument.Selection;
            }
        }

        #endregion

        #region ctor
        public MainViewModel()
        {
            MoveToTopOfBlockCmd = new DelegateCommand((o) => MoveToTopOrBottomOfBlock(true));
            MoveToBottomOfBlockCmd = new DelegateCommand((o) => MoveToTopOrBottomOfBlock(false));
            ExecuteCmd = new DelegateCommand((o) => Execute(SelectedItem));

            Init();
        }
        #endregion

        #region Methods

        private void MoveToTopOrBottomOfBlock(bool up)
        {
            try
            {
                if (DTE == null)
                {
                    return;
                }

                CodeFunction func = Selection.ActivePoint.CodeElement[vsCMElement.vsCMElementFunction] as CodeFunction;

                if (func != null)
                {
                    if (up)
                    {
                        Selection.MoveToPoint(func.StartPoint);
                    }
                    else
                    {
                        Selection.MoveToPoint(func.EndPoint);
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }

        }

        private void Execute(BaseComment cmt)
        {
            if (cmt == null)
            {
                return;
            }
            try
            {
                cmt.Execute(DTE);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }
        }

        private void Init()
        {
            //设置目录
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog("D:\\plugin\\"));

            _container = new CompositionContainer(catalog);
            try
            {
                this._container.ComposeParts(this);
            }
            catch (CompositionException compositionException)
            {
                Console.WriteLine(compositionException.ToString());
            }

            //把新的注释绑定到集合中，让界面上能够显示
            foreach (BaseComment itm in Comments)
            {
                CMTCollection.Add(itm);
            }

            if (Comments != null && Comments.Count() > 0)
            {
                SelectedItem = CMTCollection[0];
            }
        }
        #endregion
    }
}
